<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="来档单位" prop="comingUnits">
        <el-input
          v-model="queryParams.comingUnits"
          placeholder="请输入来档单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="转递方式" prop="transmittingType">
        <el-select v-model="queryParams.transmittingType" placeholder="请选择转递方式" clearable size="small">
          <el-option
            v-for="dict in transmittingTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="转递时间" prop="transmittingDate">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.transmittingDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择转递时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="送到人" prop="deliveredPeople">
        <el-input
          v-model="queryParams.deliveredPeople"
          placeholder="请输入送到人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接收人" prop="recipients">
        <user-select v-model="queryParams.recipients" multiple placeholder="请选择接收人" :max-show="1"></user-select>
<!--        <el-input-->
<!--          v-model="queryParams.recipients"-->
<!--          placeholder="请输入接收人 人员表"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:form:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:form:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:form:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:form:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="formList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="来档单位" align="center" prop="comingUnits" />
      <el-table-column label="转递方式" align="center" prop="transmittingType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.delivery_method" :value="scope.row.transmittingType"/>
        </template>
      </el-table-column>
      <el-table-column label="转递时间" align="center" prop="transmittingDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.transmittingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="送到人" align="center" prop="deliveredPeople" />
      <el-table-column label="接收人" align="center" prop="recipientsName" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="openNewTab(scope.row)"
            v-hasPermi="['business:form:edit']"
          >档案登记</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:form:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:form:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改外单位转入对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="来档单位" prop="comingUnits">
              <el-input v-model="form.comingUnits" placeholder="请输入来档单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="转递方式" prop="transmittingType">
              <el-select v-model="form.transmittingType" placeholder="请选择转递方式" style="width: 100%;">
                <el-option
                  v-for="dict in transmittingTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="转递时间" prop="transmittingDate">
              <el-date-picker clearable size="small" style="width: 100%;"
                v-model="form.transmittingDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择转递时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="送到人" prop="deliveredPeople">
              <el-input v-model="form.deliveredPeople" placeholder="请输入送到人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="接收人" prop="recipients">
              <user-select v-model="form.recipients" multiple placeholder="请选择接收人" :max-show="1"></user-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="附件" prop="attachments">
              <file-upload v-model="form.attachments"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addForm, delForm, exportForm, getForm, listForm, updateForm} from "@/api/business/form";
import UserSelect from "@/components/UserSelect/index.vue";

export default {
  name: "Form",
  components: {UserSelect},
  dicts: ['delivery_method'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外单位转入表格数据
      formList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 转递方式字典
      transmittingTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        comingUnits: null,
        transmittingType: null,
        transmittingDate: null,
        deliveredPeople: null,
        recipients: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("delivery_method").then(response => {
      this.transmittingTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询外单位转入列表 */
    getList() {
      this.loading = true;
      listForm(this.queryParams).then(response => {
        this.formList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 转递方式字典翻译
    transmittingTypeFormat(row, column) {
      return this.selectDictLabel(this.transmittingTypeOptions, row.transmittingType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        comingUnits: null,
        transmittingType: null,
        transmittingDate: null,
        deliveredPeople: null,
        recipients: null,
        remarks: null,
        attachments: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加外单位转入";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getForm(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改外单位转入";
      });
    },
    openNewTab  (row) {
      const fromId = row.id;
      this.$tab.openPage("新增[" + row.comingUnits + "]转入", '/person/from_person/index/'+fromId);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateForm(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addForm(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除外单位转入编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delForm(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有外单位转入数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportForm(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
